home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.table;
-
- import com.sun.java.accessibility.Accessible;
- import com.sun.java.accessibility.AccessibleContext;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JTable;
- import com.sun.java.swing.ToolTipManager;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.event.ChangeEvent;
- import com.sun.java.swing.event.ListSelectionEvent;
- import com.sun.java.swing.event.TableColumnModelEvent;
- import com.sun.java.swing.event.TableColumnModelListener;
- import com.sun.java.swing.plaf.TableHeaderUI;
- import java.awt.AWTEvent;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.event.InputEvent;
- import java.awt.event.MouseEvent;
- import java.util.Enumeration;
-
- public class JTableHeader extends JComponent implements TableColumnModelListener, Accessible {
- protected JTable table;
- protected TableColumnModel columnModel;
- protected boolean reorderingAllowed;
- protected boolean resizingAllowed;
- protected boolean updateTableInRealTime;
- protected transient TableColumn resizingColumn;
- protected transient TableColumn draggedColumn;
- protected transient int draggedDistance;
-
- public JTableHeader() {
- this((TableColumnModel)null);
- }
-
- public JTableHeader(TableColumnModel cm) {
- if (cm == null) {
- cm = this.createDefaultColumnModel();
- }
-
- this.setColumnModel(cm);
- this.initializeLocalVars();
- this.updateUI();
- }
-
- public void columnAdded(TableColumnModelEvent e) {
- this.resizeAndRepaint();
- }
-
- public int columnAtPoint(Point point) {
- return this.getColumnModel().getColumnIndexAtX(point.x);
- }
-
- public void columnMarginChanged(ChangeEvent e) {
- this.resizeAndRepaint();
- }
-
- public void columnMoved(TableColumnModelEvent e) {
- ((Component)this).repaint();
- }
-
- public void columnRemoved(TableColumnModelEvent e) {
- this.resizeAndRepaint();
- }
-
- public void columnSelectionChanged(ListSelectionEvent e) {
- }
-
- protected TableColumnModel createDefaultColumnModel() {
- return new DefaultTableColumnModel();
- }
-
- public AccessibleContext getAccessibleContext() {
- if (super.accessibleContext == null) {
- super.accessibleContext = new AccessibleJTableHeader(this);
- }
-
- return super.accessibleContext;
- }
-
- public TableColumnModel getColumnModel() {
- return this.columnModel;
- }
-
- public TableColumn getDraggedColumn() {
- return this.draggedColumn;
- }
-
- public int getDraggedDistance() {
- return this.draggedDistance;
- }
-
- public Rectangle getHeaderRect(int columnIndex) {
- TableColumnModel columnModel = this.getColumnModel();
- if (columnIndex >= 0 && columnIndex < columnModel.getColumnCount()) {
- int rectX = 0;
- int column = 0;
- int columnMargin = this.getColumnModel().getColumnMargin();
-
- for(Enumeration enumeration = this.getColumnModel().getColumns(); enumeration.hasMoreElements(); ++column) {
- TableColumn aColumn = (TableColumn)enumeration.nextElement();
- if (column == columnIndex) {
- return new Rectangle(rectX, 0, aColumn.getWidth() + columnMargin, ((Component)this).getSize().height);
- }
-
- rectX += aColumn.getWidth() + columnMargin;
- }
-
- return new Rectangle();
- } else {
- throw new IllegalArgumentException("Column index out of range");
- }
- }
-
- public boolean getReorderingAllowed() {
- return this.reorderingAllowed;
- }
-
- public boolean getResizingAllowed() {
- return this.resizingAllowed;
- }
-
- public TableColumn getResizingColumn() {
- return this.resizingColumn;
- }
-
- public JTable getTable() {
- return this.table;
- }
-
- public String getToolTipText(MouseEvent event) {
- String tip = null;
- Point p = event.getPoint();
- int column;
- if ((column = this.columnModel.getColumnIndexAtX(p.x)) != -1) {
- TableColumn aColumn = this.columnModel.getColumn(column);
- TableCellRenderer renderer = aColumn.getHeaderRenderer();
- Component component = renderer.getTableCellRendererComponent(this.getTable(), aColumn.getHeaderValue(), false, false, -1, column);
- if (component instanceof JComponent) {
- Rectangle cellRect = this.getHeaderRect(column);
- p.translate(-cellRect.x, -cellRect.y);
- MouseEvent newEvent = new MouseEvent(component, ((AWTEvent)event).getID(), ((InputEvent)event).getWhen(), ((InputEvent)event).getModifiers(), p.x, p.y, event.getClickCount(), event.isPopupTrigger());
- tip = ((JComponent)component).getToolTipText(newEvent);
- }
- }
-
- if (tip == null) {
- tip = ((JComponent)this).getToolTipText();
- }
-
- return tip;
- }
-
- public TableHeaderUI getUI() {
- return (TableHeaderUI)super.ui;
- }
-
- public String getUIClassID() {
- return "TableHeaderUI";
- }
-
- public boolean getUpdateTableInRealTime() {
- return this.updateTableInRealTime;
- }
-
- protected void initializeLocalVars() {
- this.table = null;
- this.reorderingAllowed = true;
- this.resizingAllowed = true;
- this.draggedColumn = null;
- this.draggedDistance = 0;
- this.resizingColumn = null;
- this.updateTableInRealTime = true;
- ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
- toolTipManager.registerComponent(this);
- }
-
- public boolean isOpaque() {
- return true;
- }
-
- public void resizeAndRepaint() {
- ((JComponent)this).revalidate();
- ((Component)this).repaint();
- }
-
- public void setColumnModel(TableColumnModel newModel) {
- if (newModel == null) {
- throw new IllegalArgumentException("Cannot set a null ColumnModel");
- } else {
- TableColumnModel oldModel = this.columnModel;
- if (newModel != oldModel) {
- if (oldModel != null) {
- oldModel.removeColumnModelListener(this);
- }
-
- this.columnModel = newModel;
- newModel.addColumnModelListener(this);
- this.resizeAndRepaint();
- }
-
- }
- }
-
- public void setDraggedColumn(TableColumn aColumn) {
- this.draggedColumn = aColumn;
- }
-
- public void setDraggedDistance(int distance) {
- this.draggedDistance = distance;
- }
-
- public void setReorderingAllowed(boolean b) {
- this.reorderingAllowed = b;
- }
-
- public void setResizingAllowed(boolean b) {
- this.resizingAllowed = b;
- }
-
- public void setResizingColumn(TableColumn aColumn) {
- this.resizingColumn = aColumn;
- }
-
- public void setTable(JTable aTable) {
- this.table = aTable;
- }
-
- public void setUI(TableHeaderUI ui) {
- if (super.ui != ui) {
- super.setUI(ui);
- ((Component)this).repaint();
- }
-
- }
-
- public void setUpdateTableInRealTime(boolean flag) {
- this.updateTableInRealTime = flag;
- }
-
- public void updateUI() {
- this.setUI((TableHeaderUI)UIManager.getUI(this));
- this.resizeAndRepaint();
- ((Container)this).invalidate();
- }
- }
-